﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EditHeightAndWeightUI : MonoBehaviour
{
    public EditHeightUI heightUI;
    public EditWeightUI weightUI;
    public EditScrollSelectUI maritalStatusUI;
    public EditJobUI jobUI;
    public LocationUI hometownUI;

    float m_height;
    float m_weight;
    int m_maritalStatus = -1;
    string m_hometownProvince;
    string m_hometownCity;
    string m_job;

    public NextBtn nextBtn;

    public Text heightText;
    public Text weightText;
    public Text maritalStatusText;
    public Text jobText;
    public Text hometownBtnText;

    internal Action<float,float,int,string, string,string> onEditEnd;

    int m_Step;

    public const int Step_First = 0;
    public const int Step_Second = 1;
    public const int Step_Third = 2;
    public const int Step_Forth = 3;
    public const int Step_Fifth = 4;

    void OnEnable()
    {
        m_maritalStatus = -1;
        m_hometownCity = "";
        m_height = 0;
        m_weight = 0;
        m_job = "";

        m_Step = 0;

        ShowObj(m_Step);

        heightUI.onEditEnd = OnEditHeightEnd;
        weightUI.onEditEnd = OnEditWightEnd;
        maritalStatusUI.onEditEnd = OnEditMaritalStatusEnd;
        jobUI.onEditEnd = OnEditJobEnd;
        hometownUI.Init(OnSelectCity);

        nextBtn.onClick = this.OnClickNext;
        if(EditDetailInfoPanel.iconSetted)
            nextBtn.SetText("下一步");
        else
            nextBtn.SetText("下一步(1/2)");

        nextBtn.Disactive();
    }

    private void OnEditHeightEnd(int index,string value)
    {
        int.TryParse(value.Trim().Replace("cm", ""),out int height);
        m_height = height;

        m_Step++;
        CheckEnd();
    }
    private void OnEditWightEnd(int index, string value)
    {
        int.TryParse(value.Trim().Replace("kg", ""), out int weight);
        m_weight = weight;

        m_Step++;
        CheckEnd();
    }
    private void OnEditMaritalStatusEnd(int index, string value)
    {
        m_maritalStatus = index;

        m_Step++;
        CheckEnd();
    }
    private void OnEditJobEnd(int index, string value)
    {
        m_job = value;

        m_Step++;
        CheckEnd();
    }
    private void OnSelectCity(string province,string location)
    {
        m_hometownProvince = province;
        m_hometownCity = location;
        hometownBtnText.text = location;
        //hometownBtnText.color = Color.black;

        m_Step++;
        CheckEnd();
    }
    void CheckEnd()
    {
        ShowObj(m_Step);

        if (this.m_maritalStatus >= 0 && !string.IsNullOrEmpty(m_hometownCity)
           && m_height > 0 && m_weight > 0 && !string.IsNullOrEmpty(m_job))
        {
            nextBtn.Active();
        }
    }
    void ShowObj(int step)
    {
        this.heightUI.gameObject.SetActive(step == Step_First);
        this.weightUI.gameObject.SetActive(step == Step_Second);
        this.hometownUI.gameObject.SetActive(step == Step_Third);
        this.jobUI.gameObject.SetActive(step == Step_Forth);
        this.maritalStatusUI.gameObject.SetActive(step == Step_Fifth);
    }

    public void OnClickShowHeightUI()
    {
        heightUI.gameObject.SetActive(true);
    }
    public void OnClickShowWeightUI()
    {
        weightUI.gameObject.SetActive(true);
    }
    public void OnClickShowMaritalStatusUI()
    {
        maritalStatusUI.gameObject.SetActive(true);
    }
    public void OnClickShowLocationUI()
    {
        hometownUI.gameObject.SetActive(true);
    }
    public void OnClickShowJobUI()
    {
        jobUI.gameObject.SetActive(true);
    }

    public void OnClickNext()
    {
        onEditEnd?.Invoke(m_height,m_weight,m_maritalStatus, m_hometownProvince,m_hometownCity,m_job);
    }
}
